/**
 * 权限控制工具
 */

import type { ZhuMember } from '../types/zhu-family';

export interface User {
  id: string;
  role: 'crown-prince' | 'emperor' | 'noble' | 'prince' | 'royal';
  kingdomId: string;
}

/**
 * 检查是否可以编辑成员信息
 */
export function canEditMember(member: ZhuMember, currentUser: User): boolean {
  // 皇帝可以编辑所有人
  if (currentUser.role === 'emperor') {
    return true;
  }

  // 只能编辑自己
  if (member.id === currentUser.id) {
    return true;
  }

  return false;
}

/**
 * 检查是否可以发起提案
 */
export function canCreateProposal(currentUser: User): boolean {
  // 皇帝、太子、藩王可以发起提案
  return ['crown-prince', 'emperor', 'prince'].includes(currentUser.role);
}

/**
 * 获取投票权重
 */
export function getVoteWeight(currentUser: User): number {
  const weights: Record<string, number> = {
    emperor: 3,
    'crown-prince': 2,
    prince: 2,
    royal: 1,
    noble: 1,
  };
  return weights[currentUser.role] || 0;
}

/**
 * 检查是否可以访问知识库
 */
export function canAccessKnowledge(knowledge: any, currentUser: User): boolean {
  if (knowledge.visibility === 'public') {
    return true;
  }

  if (knowledge.visibility === 'family') {
    return currentUser.id !== '';
  }

  // private - 只有作者可见
  return knowledge.authorId === currentUser.id;
}

/**
 * 检查是否可以多签
 */
export function canMultiSign(currentUser: User): boolean {
  // 皇帝和藩王可以参与多签
  return ['emperor', 'prince'].includes(currentUser.role);
}
